const {
  getRandomAnimal,
  getAnimalTypeService,
  getAnimalsByType,
  likeSearchAnimal,
  getAnimalById,
} = require("../service/animal.service");

class AnimalController {
  async getRandom(ctx, next) {
    // console.log("sss", ctx.ip);
    const quantity = ctx.query.quantity;
    console.log(quantity);

    const result = await getRandomAnimal(quantity);

    //4.查看存储的结果，·告知前端创建成
    ctx.body = {
      code: 200,
      message: `随机${quantity}条获取动物数据成功~`,
      data: result,
    };
  }

  async getAnimalTypes(ctx, next) {
    // console.log(ctx.query);
    // const { offset, limit } = ctx.query;
    const result = await getAnimalTypeService();
    // console.log("controller", result);
    ctx.body = {
      code: 200,
      message: `获取动物类型成功~`,
      data: result,
    };
  }

  async getAnimalsByType(ctx, next) {
    const { typeId } = ctx.params;
    const { limit, offset } = ctx.query;
    const result = await getAnimalsByType(typeId, limit, offset);
    // console.log("controller", result);
    ctx.body = {
      code: 200,
      message: `获取动物数据成功~`,
      data: result,
    };
  }

  async likeSearchAnimal(ctx, next) {
    const { keyWord } = ctx.params;
    const { limit, offset } = ctx.query;
    // console.log(keyWord);
    const result = await likeSearchAnimal(keyWord, limit, offset);
    // console.log("controller", result);
    ctx.body = {
      code: 200,
      message: `搜索动物数据成功~`,
      data: result,
    };
  }

  async getAnimalById(ctx, next) {
    const { animalId } = ctx.params;
    // console.log(keyWord);
    const result = await getAnimalById(animalId);
    // console.log("controller", result);
    ctx.body = {
      code: 200,
      message: `获取动物${animalId}数据成功~`,
      data: result,
    };
  }
}

module.exports = new AnimalController();
